﻿using Microsoft.UI.Composition.SystemBackdrops;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Text;
using WinRT;

namespace FASTControlUI
{

    internal class BackStyleControl
    {
        WindowsSystemDispatcherQueueHelper m_wsdqHelper; // See below for implementation.
        MicaController m_backdropController;
        DesktopAcrylicController m_AcrylicbackdropController;
        SystemBackdropConfiguration m_configurationSource;
        Window window;
        public enum FastStyle
        {
            Mica,
            Acrylic
        };
        public BackStyleControl(FastStyle style, ref Window window)
        {
            this.window = window;
            TrySetSystemBackdrop(style);
        }
        bool TrySetSystemBackdrop(FastStyle style)
        {
            // Create the policy object.
            m_configurationSource = new SystemBackdropConfiguration();
            if (Microsoft.UI.Composition.SystemBackdrops.MicaController.IsSupported())
            {
                m_wsdqHelper = new WindowsSystemDispatcherQueueHelper();
                m_wsdqHelper.EnsureWindowsSystemDispatcherQueueController();

                // Initial configuration state.
                m_configurationSource.IsInputActive = true;
                switch (style)
                {
                    case FastStyle.Mica:
                        m_backdropController = new MicaController();
                        // Enable the system backdrop.
                        // Note: Be sure to have "using WinRT;" to support the Window.As<...>() call.
                        m_backdropController.AddSystemBackdropTarget(window.As<Microsoft.UI.Composition.ICompositionSupportsSystemBackdrop>());
                        m_backdropController.SetSystemBackdropConfiguration(m_configurationSource);
                        break;
                    case FastStyle.Acrylic:
                        m_AcrylicbackdropController = new DesktopAcrylicController();
                        m_AcrylicbackdropController.AddSystemBackdropTarget(window.As<Microsoft.UI.Composition.ICompositionSupportsSystemBackdrop>());
                        m_AcrylicbackdropController.SetSystemBackdropConfiguration(m_configurationSource);
                        break;
                }
                return true; // succeeded
            }

            return false; // Mica is not supported on this system
        }
        public bool IsInputActive
        {
            get
            {
                return m_configurationSource.IsInputActive;
            }
            set
            {
                m_configurationSource.IsInputActive = value;
            }
        }
        public void WindowsColose()
        {
            if (m_backdropController != null)
            {
                m_backdropController.Dispose();
                m_backdropController = null;
            }
            if (m_AcrylicbackdropController != null)
            {
                m_AcrylicbackdropController.Dispose();
                m_AcrylicbackdropController = null;
            }
            m_configurationSource = null;
        }
        public void SetConfigurationSourceTheme(ElementTheme elementTheme)
        {
            switch (elementTheme)
            {
                case ElementTheme.Dark: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Dark; break;
                case ElementTheme.Light: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Light; break;
                case ElementTheme.Default: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Default; break;
            }
        }
    }
}
